package com.kcwl.idea.plugins.docconverter;

import com.kcwl.idea.plugins.base.JavadocUtil;
import com.kcwl.idea.plugins.base.constants.SwaggerConstants;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import com.intellij.psi.*;

import javax.swing.*;
import java.util.Objects;

/**
 * java 注释转 swagger
 */
public class JavaDocToSwaggerAction extends AnAction {



    @Override
    public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
        int choice = JOptionPane.showConfirmDialog(null, "javadoc转swagger？" , "确认", JOptionPane.YES_NO_OPTION);
        if (choice != JOptionPane.YES_OPTION) {
            return;
        }
        Project project = anActionEvent.getProject();
        // 获取当前编辑器对象
        Editor editor = anActionEvent.getData(CommonDataKeys.EDITOR);
        if (editor != null) {
            // 获取当前光标位置所在的PsiElement
            PsiElement psiElement = DocUtil.getPsiElementAtCaret(editor, anActionEvent.getProject());
            if (psiElement != null) {
                // 获取包含该PsiElement的PsiClass
                PsiClass psiClass = PsiTreeUtil.getParentOfType(psiElement, PsiClass.class);
                if (psiClass != null) {
                    // 获取 PsiElementFactory 对象
                    JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
                    PsiElementFactory elementFactory = psiFacade.getElementFactory();
                    // 打印类名
                    try {
                        PsiField[] allFields = psiClass.getFields();
                        WriteCommandAction.runWriteCommandAction(project, () -> {
                            importPkg(psiClass, SwaggerConstants.ApiModelProperty);
                            for (PsiField field : allFields) {
                                PsiDocComment fieldDocComment = field.getDocComment();
                                if (fieldDocComment != null) {
                                    String descriptionText = DocUtil.getDescriptionText(field.getDocComment());
                                    //删除字段上原来的注释
                                    fieldDocComment.delete();

                                    PsiModifierList modifierList = field.getModifierList();
                                    boolean hasAnnotation = modifierList.findAnnotation(SwaggerConstants.ApiModelProperty) != null;
                                    if (!hasAnnotation) {
                                        PsiAnnotation annotation = elementFactory.createAnnotationFromText("@ApiModelProperty(value = \"" + descriptionText + "\")", field);
                                        // 添加注解到字段上
                                        field.addBefore(annotation, field.getFirstChild());
                                    }
                                }
                            }
                            PsiDocComment classDocComment = psiClass.getDocComment();
                            if (classDocComment != null) {
                                String descriptionText = DocUtil.getDescriptionText(classDocComment);
                                classDocComment.delete();
                                // 将注解添加到类的注解列表中
                                PsiModifierList modifierList = psiClass.getModifierList();
                                boolean hasAnnotation = modifierList.findAnnotation(SwaggerConstants.ApiModel) != null;
                                if (!hasAnnotation) {
                                    PsiAnnotation annotation = elementFactory.createAnnotationFromText("@ApiModel(value = \"" + descriptionText + "\")", psiClass);
                                    modifierList.addAfter(annotation, null);
                                }
                                //导入包
                                importPkg(psiClass, SwaggerConstants.ApiModel);
                            }
                            PsiJavaFile psiFile = (PsiJavaFile) psiClass.getContainingFile();
                            JavadocUtil.codeFormat(project, psiFile);
                        });
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }

    }

    private void importPkg(PsiClass psiClass, String pkg) {
        PsiJavaFile psiFile = (PsiJavaFile) psiClass.getContainingFile();
        if (!DocUtil.hasImportPkg(psiFile, pkg)) {
            PsiImportStatement importStatement = JavaPsiFacade.getElementFactory(psiClass.getProject())
                    .createImportStatement(Objects.requireNonNull(JavaPsiFacade.getInstance(psiClass.getProject())
                            .findClass(pkg, psiClass.getResolveScope())));
            psiFile.getImportList().add(importStatement);
        }
    }

}
